//
//  WEXPictureView.swift
//  microBlog
//
//  Created by apple on 16/4/22.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit

//每一个item之间的间距
let statusItemMargin: CGFloat = 5

//每一个item 的 宽高
let statusItemWH: CGFloat = (SCREENWIDTH - 2 * commonMargin - 2 * statusItemMargin) / 3

let PicIdentifier: String = "picCell"

class WEXPictureView: UICollectionView {
    

//    元组数据
    var dataInfo:(pic_urls: [WEXPicInfoModel], size: CGSize )? {
    
        didSet{
            
            if dataInfo?.pic_urls.count == 1 {
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = dataInfo?.size ?? CGSizeZero
            
            }else{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = CGSize(width: statusItemWH, height: statusItemWH)
                
            }
            
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(dataInfo?.size ?? CGSizeZero)
                
            }
            
            reloadData()
            
        }
    
    }
    
    
//    给外部传输数值的属性
//    var pic_urls: [WEXPicInfoModel]? {
//        
//        didSet{
//            
//            lb_number.text = "\(pic_urls?.count ?? 0)"
//            
//            reloadData()
//        }
//    }
    
//    初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
        
//        注册cell
        registerClass(WEXPictureCollectionCell.self, forCellWithReuseIdentifier: PicIdentifier)
        
//        设置layout
       let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: statusItemWH, height: statusItemWH)
        
        layout.minimumInteritemSpacing = statusItemMargin
        layout.minimumLineSpacing = statusItemMargin
        
        addSubview(lb_number)
        
        
        
        lb_number.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
//        设置当前view为自己的数据源代理
        dataSource = self
        delegate = self
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor.orangeColor()
        
    }
    
    // MARK: - 懒加载子控件
    private var lb_number: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 20)
    
    
}

// MARK: - collectionView的数据源方法
extension WEXPictureView: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataInfo?.pic_urls.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PicIdentifier, forIndexPath: indexPath) as! WEXPictureCollectionCell
        
        cell.pictureInfo = dataInfo!.pic_urls[indexPath.row]
        
        return cell
        
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let photoBrowser = SDPhotoBrowser()
        
        photoBrowser.delegate = self
        
        photoBrowser.currentImageIndex = indexPath.item
//        设置图片总张数
        photoBrowser.imageCount = dataInfo!.pic_urls.count
        
        photoBrowser.sourceImagesContainerView = self
        
        photoBrowser.show()
        
    }
    
}

// MARK: - SDPhotoBrowser的代理方法
extension WEXPictureView : SDPhotoBrowserDelegate {
    
//    获取当前cell 的图片
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        
        let cell = self.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0))! as!WEXPictureCollectionCell
        
        return cell.igv_picture.image
        
        
    }
    
    
//    获取高清大图的地址
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        
        
        let urlString = dataInfo!.pic_urls[index].thumbnail_pic!.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        print(urlString)
        
        return NSURL(string: urlString)

    }

}


// MARK: - 自定义cell
class WEXPictureCollectionCell: UICollectionViewCell {
    
    var pictureInfo: WEXPicInfoModel? {
        
        didSet{
            
            igv_picture.sd_setImageWithURL(NSURL(string: pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            
            igv_gifPicture.hidden = !(pictureInfo?.thumbnail_pic ?? "").hasSuffix(".gif")
            
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
//        添加子控件
        contentView.addSubview(igv_picture)
        
        contentView.addSubview(igv_gifPicture)
        
        
//        布局子控件
        igv_picture.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
            
        }
        
        igv_gifPicture.snp_makeConstraints { (make) -> Void in
            
            make.trailing.bottom.equalTo(contentView)
        }
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        
        
    }
    
    
    // MARK: - 懒加载子控件
    private lazy var igv_picture: UIImageView = {
       
        let picture = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        picture.contentMode = .ScaleAspectFill
        
        picture.clipsToBounds = true
        
        return picture
        
    }()
    
    private lazy var igv_gifPicture: UIImageView = {
        
        let gif = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        return gif
    }()
    
    
}